package practiceProjects;

import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;

/**
 * @Project ：learn_java
 * @File ：CalcYearsDesignatedByHeavenlyStemsAndEarthlyBranches.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/5/22 20:20
 * @Description ：干支纪年计算
 */
public class CalcYearsDesignatedByHeavenlyStemsAndEarthlyBranches {

    /**
     * 天干
     */
    private static final Map<Character, Integer> stems = new HashMap<>(){{
        put('甲', 4);
        put('乙', 5);
        put('丙', 6);
        put('丁', 7);
        put('戊', 8);
        put('己', 9);
        put('庚', 0);
        put('辛', 1);
        put('壬', 2);
        put('癸', 3);
    }};

    /**
     * 地支
     */
    private static final Map<Character, Integer> branches = new HashMap<>(){{
        put('子', 4);
        put('丑', 5);
        put('寅', 6);
        put('卯', 7);
        put('辰', 8);
        put('巳', 9);
        put('午', 10);
        put('未', 11);
        put('申', 0);
        put('酉', 1);
        put('戌', 2);
        put('亥', 3);
    }};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = null;
        boolean validInput = false;

        while (!validInput) {
            try {
                input = String.valueOf(scanner.nextInt());
                validInput = true;
            } catch (Exception e) {
                throw new InputMismatchException("请输入有效年份");
            }
        }
        int year = Integer.parseInt(input);

        /* 计算部分 */
        String r = "";
        // 计算天干
        for (Map.Entry<Character, Integer> entry: stems.entrySet()) {
            if (entry.getValue().equals(year % 60)) {
                r += entry.getKey();
                break;
            }
        }
        // 计算地支
        for (Map.Entry<Character, Integer> entry: branches.entrySet()) {
            if (entry.getValue().equals(year % 12)) {
                r += entry.getKey();
                break;
            }
        }

        System.out.println(input + "年的结果是：" + r);
    }
}
